package com.example.wwj.controller;

import com.example.wwj.pojo.Curriculum;
import com.example.wwj.pojo.Score;
import com.example.wwj.service.ScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/score")
public class scoreController {

    @Autowired
    private ScoreService scoreService;

    /*
    * 查询所有的学生成绩信息
    * 可用
    * */
    @GetMapping("/scorelist")
    public List<Score> getScoreList(){
        return scoreService.scoreList();
    }

    /**
     * 学生成绩信息分页查询
     * pageNum:页码
     * pageSize:一页几个数据
     * total：数据总数
     * classId:班级id
     * gradeId:年级id
     * teacher_name:老师姓名
     * student_name:学生姓名
     */
    @GetMapping("/page")
    public Map<String, Object> findPage(
            @RequestParam(required = false) Integer pageNum,
            @RequestParam(required = false) Integer pageSize,
            @RequestParam(required = false) Integer class_id,
            @RequestParam(required = false) Integer grade_id,
            @RequestParam(required = false) String teacher_name,
            @RequestParam(required = false) String student_name
    ) {
        if (pageNum == null || pageSize == null) {
            // 处理缺少参数的情况，这里可以返回错误信息或者设置默认值
        }

        pageNum = (pageNum - 1) * pageSize;

        List<Score> data;
        Integer total;

        if (class_id != null || grade_id != null || (teacher_name != null && !teacher_name.isEmpty()) || (student_name != null && !student_name.isEmpty())) {
            data = scoreService.selectPage(pageNum, pageSize, class_id, grade_id, teacher_name,student_name);
            total = scoreService.selectTotal(class_id, grade_id, teacher_name,student_name);
        } else {
            data = scoreService.selectPage(pageNum, pageSize, null, null, null,null); // 查询所有数据
            total = scoreService.selectTotal(class_id, grade_id, teacher_name,student_name);
        }

        Map<String, Object> res = new HashMap<>();
        res.put("data", data);
        res.put("total", total);
        return res;
    }
    /*
     * 批量删除
     * ids:学生的id的列表
     * */
    @GetMapping("/delete")
    public int delete(@RequestParam("ids") List<Integer> ids) {
        return scoreService.deleteById(ids);
    }

}
